// register.js
var app = getApp()
import { showTi } from "../../utils/modal.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    register:{
      real_name:"",
      tel:"",
      password:"",
      role:"service",
      remote_headimg_url:"",
      code:"",
      mp_open_id:""
    },
    repeatPassword:"",
    issend:false,
    time:60
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    app.getUserInfo((user) => {
      self.data.register.real_name = user.nickName
      self.data.register.remote_headimg_url = user.avatarUrl
      console.log(self.data.register);
    })
    app.getOpenId(id => {
      self.data.register.mp_open_id = id;
    })
    var Statistics_Website_logo = {
      'Website_logo_title': '学而思',
      'Website_logo_Theme': '教育行业',
      'Website_logo_Company': '好未来'
    }
  },
  registerTel:function(e){
    this.data.register.tel=e.detail.value
  },
  registerPass:function(e){
    this.data.register.password=e.detail.value
  },
  registerRepPass:function(e){
    this.data.repeatPassword=e.detail.value
  },
  registerCode: function (e) {
    this.data.register.code = e.detail.value
  },
  //发送验证码
  send:function(){
    let self=this
    if (!/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.data.register.tel)) {
      showTi("请输入正确的手机号码")
    }else{
      app.query('web/send_code', { tel: this.data.register.tel }, 'post').then(res => {
        if (res.code === 200) {
          console.log(res);
          self.data.register.code = res.val_code
          this.timeDown();
        } else {
          showTi(res.msg)
        }
      })
    }
  },
  //验证码倒计时
  timeDown:function(){
    this.setData({"issend":true})
    let time=60;
    let timer=setInterval(()=>{
      --time;
      this.setData({"time":time})
      if(time<=0){
       this.setData({"issend":false})
        clearInterval(timer)
      }
    },1000)
  },
  //注册
  register:function(){
    let self = this
    if (!this.data.register.code){
      showTi("请输入验证码")
    }else{
      app.query('web/sign_service', self.data.register, 'post').then(res => {
        if (res.code === 200) {
          console.log(res);
          app.globalData.userInfo = res.user
          wx.setStorageSync('wechat_service_5mutian_token', res.token)
          wx.showModal({
            title: '提示',
            content: '注册成功,请实名认证',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({ url: '../nameAuthentication/nameAuthentication' })
              }
            }
          })     
        } else {
          showTi(res.msg)
        }
      })
    }
  },

})